//
//  DownLoadView.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/7.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "DownLoadView.h"

@implementation DownLoadView
- (instancetype)initWithFrame:(CGRect)frame
{
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        
        UIView *alphaView = [[UIView alloc] init];
        alphaView.backgroundColor = [CSBlackColor colorWithAlphaComponent:0.7];
        [self addSubview:alphaView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenViews)];
        [alphaView addGestureRecognizer:tap];
        self.alphaView = alphaView;
        
        UIView *whiteView = [[UIView alloc] init];
        whiteView.layer.cornerRadius = 5;
        whiteView.backgroundColor = [CSWhiteColor colorWithAlphaComponent:1];
        [self addSubview:whiteView];
        self.whiteView = whiteView;
        
        
        /**
         下载进度
         */
        UILabel *loadLable = [[UILabel alloc] init];
        loadLable.text = @"正在下载中68%";
        loadLable.font = MidFont;
        loadLable.textColor = CSBlackColor1;
        [whiteView addSubview:loadLable];
        self.loadLable = loadLable;
    
        
        [self setConstraints];
        
    }
    
    return self;
}

-(void)setConstraints
{
    
    
    
    /**
     背景色
     */
    [self.alphaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    /**
     内容view
     */
    [self.whiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.center.mas_equalTo(self);
        make.right.mas_equalTo(self).offset(Iphone5?-70:-85);
        make.left.mas_equalTo(self).offset(Iphone5?70:85);
        make.height.mas_equalTo(60);
    }];
    
    
    /**
     标题
     */
    [self.loadLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.whiteView);
        make.left.mas_greaterThanOrEqualTo(self.whiteView).offset(5);
        make.right.mas_lessThanOrEqualTo(self.whiteView).offset(-5);
    }];
    
    
    
    
    
    
    
}


-(void)drawRect:(CGRect)rect
{
//    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
//    gradientLayer.colors = @[
//                             (id)CSColorFromRGB(0xF52C50).CGColor,(id)CSColorFromRGB(0xF95C48).CGColor
//                             ,
//                             ];
//    gradientLayer.startPoint = CGPointMake(0, 0);
//    gradientLayer.endPoint = CGPointMake(1.0, 0);
//    gradientLayer.frame = self.openBtn.bounds;
//    [self.openBtn.layer insertSublayer:gradientLayer atIndex:0];
}


- (void)showWithAnimation {
    
    
    
    [self addViews];
    
    CGRect frame = self.whiteView.frame;
    self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    self.whiteView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        self.whiteView.y = frame.origin.y;
    }];
    
}


- (void)hiddenWithAnimation {
    CGFloat height = self.whiteView.frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self hiddenViews];
    }];
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    
    [self removeFromSuperview];
}


@end
